package com.alibaba.citrus.ots.platform.enhance.repository.impl;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.platform.enhance.repository.SalesOrderLineQueryServiceRepository;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.common.SalesOrderLineDTO;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.salesOrder.QuerySalesOrderLine;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.activity.QuerySalesOrderLineParam;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsb2btradecenter.domain.orderline.dto.QueryListByForecastSubIdsRequest;
import com.epoch.app.otsb2btradecenter.domain.orderline.dto.QueryOrderLineByGmtModifyAsc2Request;
import com.epoch.app.otsb2btradecenter.domain.orderline.dto.QueryOrderLineByGmtModifyAscRequest;
import com.epoch.app.otsb2btradecenter.domain.orderline.dto.QuerySalesOrderLineByIdsRequest;
import com.epoch.app.otsb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.otsb2btradecenter.domain.orderline.service.OrderLineService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2022/1/19
 * @Description 销售子单查询资源层
 */
@Repository
public class SalesOrderLineQueryServiceRepositoryImpl implements SalesOrderLineQueryServiceRepository {


    @Resource
    private OrderLineService orderLineService;

    @Override
    @RepositoryInvoker
    public Result<List<SalesOrderLineDTO>> querySalesOrderLine(QuerySalesOrderLineParam querySalesOrderLineParam) {

        Result<List<SalesOrderLineDTO>> result = Result.success(new ArrayList<>());
        if (Objects.isNull(querySalesOrderLineParam) || CollectionUtils.isEmpty(querySalesOrderLineParam.getQuerySalesOrderLines())) {
            return result;
        }

        QueryListByForecastSubIdsRequest request = new QueryListByForecastSubIdsRequest();

        List<QuerySalesOrderLine> querySalesOrderLines = querySalesOrderLineParam.getQuerySalesOrderLines().stream()
                .filter(e -> Objects.nonNull(e.getForecastSubOrderId())
                        && StringUtils.isNotBlank(e.getSequenceNo())).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(querySalesOrderLines)) {
            return result;
        }

        List<String> subOrderIds = querySalesOrderLines.stream().map(e -> e.getForecastSubOrderId()).collect(Collectors.toList());


        //构建入参
        //根据预报子单查询销售订单
        request.setSubOrderIds(subOrderIds);
        request.setStart(querySalesOrderLineParam.getStart());
        request.setLimit(1);
        request.setStatus(querySalesOrderLineParam.getStatus());
        Result<List<OrderLine>> listResult = orderLineService.queryListByForecastSubIds(request);

        if (Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())) {
            return result;
        }

        request.setLimit(listResult.getTotal());
        listResult = orderLineService.queryListByForecastSubIds(request);

        result.setTotal(listResult.getTotal());
        List<OrderLine> orderLineDTOS = listResult.getResult();

        List<SalesOrderLineDTO> salesOrderLineDTOSResult = Lists.newArrayList();
        result.setResult(salesOrderLineDTOSResult);

        //过滤数据
        Map<String, OrderLine> orderLineDTOMap =
                orderLineDTOS.
                        stream().filter(e -> StringUtils.isNotBlank(e.getFeatures()))
                        .filter(e -> {
                            Map map = JSONObject.parseObject(e.getFeatures(), Map.class);
                            return StringUtils.isNotBlank((String) map.get("sequenceNo"));
                        }).collect(Collectors.toMap(e -> e.getFrontOrderLineId()
                                + "_" + JSONObject.parseObject(e.getFeatures(), Map.class).get("sequenceNo"), Function.identity(), (x, y) -> x));

        for (QuerySalesOrderLine querySalesOrderLine : querySalesOrderLines) {
            OrderLine orderLine = orderLineDTOMap.get(querySalesOrderLine.getForecastSubOrderId() + "_" + querySalesOrderLine.getSequenceNo());

            if (Objects.isNull(orderLine)) {
                continue;
            }

//            String features = salesOrderLineDTO.getFeatures();
//            if (StringUtils.isBlank(features)) {
//                continue;
//            }
//            Map map = JSONObject.parseObject(features, Map.class);
//            String sequenceNo = (String) map.get("sequenceNo");
//            if (StringUtils.isBlank(sequenceNo) || !StringUtils.equals(sequenceNo, querySalesOrderLine.getSequenceNo())) {
//                continue;
//            }

            SalesOrderLineDTO salesOrderLineDTO = new SalesOrderLineDTO();
            BeanUtils.copyProperties(orderLine,salesOrderLineDTO);

            salesOrderLineDTOSResult.add(salesOrderLineDTO);
        }

        return result;
    }


    @Override
    @RepositoryInvoker
    public Result<List<SalesOrderLineDTO>> querySalesOrderLineByGmtModifyAsc(QuerySalesOrderLineParam orderLineDTO) {


        Result<List<SalesOrderLineDTO>> result = Result.success(new ArrayList<>());
        QueryOrderLineByGmtModifyAscRequest request = new QueryOrderLineByGmtModifyAscRequest();
        request.setStart(orderLineDTO.getStart());
        request.setLimit(orderLineDTO.getLimit());
        request.setStartModifyTime(orderLineDTO.getStartModifyTime());
        request.setEndModifyTime(orderLineDTO.getEndModifyTime());

        Result<List<OrderLine>> listResult = orderLineService.queryOrderLineByGmtModifyAsc(request);
        if (Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())) {
            return result;
        }

        result.setTotal(listResult.getTotal());

        result.setResult(
                listResult.getResult().stream().map(e -> {

                    SalesOrderLineDTO salesOrderLineDTO = new SalesOrderLineDTO();
                    BeanUtils.copyProperties(e, salesOrderLineDTO);
                    return salesOrderLineDTO;
                }).collect(Collectors.toList())
        );
        return result;
    }

    @Override
    @RepositoryInvoker
    public List<SalesOrderLineDTO> querySalesOrderLineByOrderId(String mainOrderId) {
        QuerySalesOrderLineByIdsRequest querySalesOrderLineByIdsRequest = new QuerySalesOrderLineByIdsRequest();
        querySalesOrderLineByIdsRequest.setMainOrderId(mainOrderId);
        Result<List<OrderLine>> listResult = orderLineService.querySalesOrderLineByIds(querySalesOrderLineByIdsRequest);
        if (Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())) {
            return new ArrayList<>();
        }
        List<SalesOrderLineDTO> salesOrderLineDTOS = listResult.getResult().stream().map(e -> {
            SalesOrderLineDTO salesOrderLineDTO = new SalesOrderLineDTO();
            BeanUtils.copyProperties(e, salesOrderLineDTO);
            return salesOrderLineDTO;
        }).collect(Collectors.toList());
        return salesOrderLineDTOS;
    }

    @Override
    @RepositoryInvoker
    public Result<List<SalesOrderLineDTO>> querySalesOrderLineByGmtModifyDesc(QuerySalesOrderLineParam orderLineParam) {


        Result<List<SalesOrderLineDTO>> result = Result.success(new ArrayList<>());
        QueryOrderLineByGmtModifyAsc2Request request = new QueryOrderLineByGmtModifyAsc2Request();
        request.setStart(orderLineParam.getStart());
        request.setLimit(orderLineParam.getLimit());
        request.setStartModifyTime(orderLineParam.getStartModifyTime());
        request.setEndModifyTime(orderLineParam.getEndModifyTime());

        Result<List<OrderLine>> listResult = orderLineService.queryOrderLineByGmtModifyAsc2(request);
        if (Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())) {
            return result;
        }

        result.setTotal(listResult.getTotal());

        result.setResult(
                listResult.getResult().stream().map(e -> {

                    SalesOrderLineDTO salesOrderLineDTO = new SalesOrderLineDTO();
                    BeanUtils.copyProperties(e, salesOrderLineDTO);
                    return salesOrderLineDTO;
                }).collect(Collectors.toList())
        );
        return result;


    }
}
